package com.ymatou.iapi.sellerproduct.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductDescExtra;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_SaveProductTemplate {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
	private static YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建/编辑商品模版");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveProductTemplateCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_001() {
		Logger.start(true, "创建商品模版");
		try{
			saveProductTemplateCall.deleteTemplate(sellerId);
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId));
			
			Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			
			JSONObject requestBean = ret.getJSONObject("JsonBeanRequest");
			Integer templateId = ret.getJSONObject("Data").getInt("ProductTemplateId");
			requestBean.put("ProductTemplateId", templateId);
			
			YmtProductDescExtra ymtProductDescExtra = ymtProductDescExtraWapper.selectByTemplateId(templateId);
			Map<String, Object> mapInSql = MapUtil.pojoToMap(ymtProductDescExtra);
			
			checkSaveProductTemplate(requestBean,mapInSql);

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_002() {
		Logger.start(true, "修改商品模版");
		try{
			saveProductTemplateCall.deleteTemplate(sellerId);
			//create
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId));
			
			Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			Integer templateId = ret.getJSONObject("Data").getInt("ProductTemplateId");
			
			//modify
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("SellerId", sellerId);
			updateMap.put("ProductTemplateId", templateId);
			updateMap.put("TemplateName", "updateName");
			updateMap.put("Description", "updateDesc");
			updateMap.put("PicUrls", new JSONArray(Arrays.asList("1","2")));
			
			JSONObject ret2 = saveProductTemplateCall.callAndGetReturnData(updateMap);
			JSONObject requestBean = ret2.getJSONObject("JsonBeanRequest");
			YmtProductDescExtra ymtProductDescExtra = ymtProductDescExtraWapper.selectByTemplateId(templateId);
			Map<String, Object> mapInSql = MapUtil.pojoToMap(ymtProductDescExtra);
			
			checkSaveProductTemplate(requestBean,mapInSql);

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_003() {
		Logger.start(true, "修改商品模版 TemplateType不能修改");
		try{
			saveProductTemplateCall.deleteTemplate(sellerId);
			//create
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId));
			
			Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			Integer templateId = ret.getJSONObject("Data").getInt("ProductTemplateId");
			
			//modify
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("SellerId", sellerId);
			updateMap.put("ProductTemplateId", templateId);
			updateMap.put("TemplateType", 5);
			saveProductTemplateCall.callAndGetReturnData(updateMap);
			
			
			JSONObject requestBean = ret.getJSONObject("JsonBeanRequest");
			requestBean.put("ProductTemplateId", templateId);
			YmtProductDescExtra ymtProductDescExtra = ymtProductDescExtraWapper.selectByTemplateId(templateId);
			Map<String, Object> mapInSql = MapUtil.pojoToMap(ymtProductDescExtra);
			
			checkSaveProductTemplate(requestBean,mapInSql);

		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_004() {
		Logger.start(true, "一个类型模板上限不超过20");
		try{
			saveProductTemplateCall.deleteTemplate(sellerId);
			for (int i=0;i<20;i++){
				JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId));
				Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			}
			
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId));
			Logger.verifyEquals(201, ret.getInt("Code"), "返回报文code");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveProductTemplate_005() {
		Logger.start(true, "模板关联多个商品，修改模板。");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			List<ProdTplBean> ProdTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			
			//发布商品
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<3;i++){
				JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc12.json");	
				Map<String, Object> updateMap = MapUtil.hashMap();
				updateMap.put("UserID", sellerId);
				updateMap.put("LoginID", sellerName);
				updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
				updateMap.put("Product->BuyerNoticeId", buyerNoticeTemplate.getTemplateId());
				updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());
				updateMap.put("Product->iThirdCategoryId", 1123);
				JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
				//call&获取返回报文
				JSONObject result = SellerProductCallService.addNewProduct(addProductBean);
				productIdList.add(result.getJSONObject("Data").getString("ProductID"));
			}
			
			
			//check product template before update template
			productIdList.forEach(productId->{
				try {
					Logger.debug("=========ProductId:[ "+ productId +" ]================");
					SellerProductCallService.checkTemplateWithProd(productId, ProdTplBeanList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			
			
			//modify
			ProdTplBeanList.forEach(prodTplBean->{
				
				String p1 = "http://pic1.ymatou.com/G01/shangou/M00/31/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
				String p2 = "http://pic1.ymatou.com/G01/shangou/M00/22/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
				List<String> picList =Arrays.asList(p1,p2);
				Map<String, Object> updateMap = MapUtil.hashMap();
				updateMap.put("SellerId", sellerId);
				updateMap.put("ProductTemplateId", prodTplBean.getTemplateId());
				updateMap.put("TemplateName", "updateName");
				updateMap.put("Description", "updateDesc");
				updateMap.put("PicUrls", new JSONArray(picList));
				saveProductTemplateCall.callAndGetReturnData(updateMap);
				
				prodTplBean.setTemplateName("updateName");
				prodTplBean.setTemplateDesc("updateDesc");
				prodTplBean.setPicUrls(picList);
		
				
			});
			
			//check product template after update template
			productIdList.forEach(productId->{
				try {
					Logger.debug("=========ProductId:[ "+ productId +" ]================");
					SellerProductCallService.checkTemplateWithProd(productId, ProdTplBeanList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	} 
	
	
	
	
	
	
	
	
	
	
	public static void checkSaveProductTemplate(JSONObject requestBean,Map<String, Object> mapInSql) throws Exception{
		//图片检查	
		JSONArray requestPics = requestBean.getJSONArray("PicUrls");
		if (mapInSql.get("picUrls")==null || mapInSql.get("picUrls").toString().equals("")){
			Logger.verifyEquals(0, requestPics.length(), "Template 图片应该不存在");
		}else{
			JSONArray templatePicsInfoInSql = new JSONArray(Arrays.asList(mapInSql.get("picUrls").toString().split(",")));
			AssertService.checkArray(templatePicsInfoInSql, requestPics,"Template 图片检查 ");
		}
		Map<String, Object> requestMap = MapUtil.hashMap();
		requestMap.put("ID", requestBean.getInt("ProductTemplateId"));
		requestMap.put("sellerId", requestBean.getString("SellerId"));
		requestMap.put("templateName", requestBean.getString("TemplateName"));
		requestMap.put("description", requestBean.getString("Description"));
		requestMap.put("templateType", requestBean.getInt("TemplateType"));
		
		AssertService.assertResultEqual(requestMap, mapInSql);
	}
	
}
